﻿using BrightIdeasSoftware;
using ComponentFactory.Krypton.Toolkit;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using TaggingToolkit.Framework;

namespace TaggingToolkit.Controls
{
    public class ColoredFilteredTextRenderer : HighlightTextRenderer
    {
        private IPalette _palette;

        public ColoredFilteredTextRenderer() : base()
        {
            _palette = KryptonManager.CurrentGlobalPalette;

            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);
        }

        public ColoredFilteredTextRenderer(TextMatchFilter filter) : this()
        {
            base.Filter = filter;
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            ListView?.Invalidate();
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            _palette = KryptonManager.CurrentGlobalPalette;

            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            ListView?.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_palette != null)
                {
                    _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);
                    _palette = null;
                }

                KryptonManager.GlobalPaletteChanged -= new EventHandler(OnGlobalPaletteChanged);
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// Return the color of the background color when the item is selected
        /// </summary>
        /// <returns>The background color of the subitem</returns>
        public override Color GetSelectedBackgroundColor()
        {
            if (this.ListView.Focused)
                return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);

            if (!this.ListView.HideSelection)
                return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);

            return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);
        }

        /// <summary>
        /// Return the color of the foreground color when the item is selected
        /// </summary>
        /// <returns>The foreground color of the subitem</returns>
        public override Color GetSelectedForegroundColor()
        {
            if (this.ListView.Focused)
                return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Normal);

            if (!this.ListView.HideSelection)
                return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Normal);

            return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Normal);
        }
    }
}
